# https://zhuanlan.zhihu.com/p/387408410

# https://blog.csdn.net/q923714892/article/details/117336593

import cv2
import numpy as np


def PerspectiveTransform(img, src, dst, result_size):
    src = np.float32(src)
    dst = np.float32(dst)
    # 生成透视变换矩阵；进行透视变换
    m = cv2.getPerspectiveTransform(src, dst)
    result = cv2.warpPerspective(img, m, result_size)
    return result

if __name__ == "__main__":
    img = cv2.imread('photo1.jpg')

    result3 = img.copy()

    #img = cv2.GaussianBlur(img,(3,3),0)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    #转灰度，做单通道计算比较节省时间
    edges = cv2.Canny(gray,50,150,apertureSize = 3)
    #canny边缘检测（仅针对这次的输入图片）
    cv2.imshow("canny", edges)

    '''
    注意这里src和dst的输入并不是图像，而是图像对应的顶点坐标。
    '''
    src = np.float32([[207, 151], [517, 285], [17, 601], [343, 731]])
    dst = np.float32([[0, 0], [337, 0], [0, 488], [337, 488]])
    # 生成透视变换矩阵；进行透视变换
    m = cv2.getPerspectiveTransform(src, dst)
    result = cv2.warpPerspective(result3, m, (337, 488))
    # (337,488)是输出图像大小
    cv2.imshow("src", img)
    cv2.imshow("result", result)
    cv2.waitKey(0)

